(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['views/base/view', 'meiomask', 'lib/form_handlebars_helpers'], function(View) {
    'use strict';
    var BaseFormView, _ref;
    return BaseFormView = (function(_super) {
      __extends(BaseFormView, _super);

      function BaseFormView() {
        _ref = BaseFormView.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      BaseFormView.prototype.successMessage = 'Registro salvo';

      BaseFormView.prototype.formSelector = 'form';

      BaseFormView.prototype.formEvent = 'submit';

      BaseFormView.prototype.firstFocus = true;

      BaseFormView.prototype.formData = {};

      BaseFormView.prototype.validationRules = null;

      BaseFormView.prototype.validationMessages = null;

      BaseFormView.prototype.validationGroups = null;

      BaseFormView.prototype.initialize = function(options) {
        var _ref1, _ref2,
          _this = this;
        if (options == null) {
          options = {};
        }
        BaseFormView.__super__.initialize.apply(this, arguments);
        _(this).extend(options);
        this.createLoader();
        if (this.model && ((_ref1 = this.model) != null ? _ref1.id : void 0) && !((_ref2 = this.model) != null ? _ref2.fetched : void 0)) {
          return this.model.fetch().always(function() {
            return _this.render();
          });
        } else {
          return this.render();
        }
      };

      BaseFormView.prototype.afterRender = function() {
        this.$form = this.$el.find(this.formSelector);
        BaseFormView.__super__.afterRender.apply(this, arguments);
        this.$form.attr('novalidate', 'novalidate');
        this.setupValidator();
        if (this.fillForm) {
          this.fillForm(this.model);
        }
        if (this.firstFocus) {
          return this.$form.find(':input:first:visible').focus();
        }
      };

      BaseFormView.prototype.render = function() {
        this.hideLoader();
        return BaseFormView.__super__.render.apply(this, arguments);
      };

      BaseFormView.prototype.cacheDOMElements = function() {
        return this.DOMElements = {
          $btnSubmit: this.$el.find('button[type="submit"]')
        };
      };

      BaseFormView.prototype.setupValidator = function(settings) {
        var hasSettings;
        if (settings == null) {
          settings = {};
        }
        hasSettings = this.validationRules || settings.rules;
        if (!hasSettings) {
          return;
        }
        this.validatorSettings || (this.validatorSettings = {});
        if (this.validationRules) {
          this.validatorSettings.rules = _.result(this, 'validationRules');
        }
        if (this.validationMessages) {
          this.validatorSettings.messages = _.result(this, 'validationMessages');
        }
        if (this.validationGroups) {
          this.validatorSettings.groups = _.result(this, 'validationGroups');
        }
        if (settings.rules) {
          _.extend(this.validatorSettings.rules, settings.rules);
        }
        if (settings.messages) {
          _.extend(this.validatorSettings.messages, settings.messages);
        }
        if (settings.groups) {
          _.extend(this.validatorSettings.groups, settings.groups);
        }
        if (!this.validator) {
          return this.validator = this.$form.validate(this.validatorSettings);
        } else {
          return _.extend(this.validator.settings, this.validatorSettings);
        }
      };

      BaseFormView.prototype.fillModel = function(form) {};

      BaseFormView.prototype.fillForm = function(model) {};

      BaseFormView.prototype.changeSubmitLabel = function(label) {
        if (label == null) {
          label = 'Salvar';
        }
        if (this.DOMElements.$btnSubmit) {
          return this.DOMElements.$btnSubmit.text(label);
        }
      };

      BaseFormView.prototype.registryDOMEvents = function() {
        return this.delegate(this.formEvent, this.formSelector, this.submitHandler);
      };

      BaseFormView.prototype.submitHandler = function(event) {
        event.preventDefault();
        event.stopPropagation();
        return this.saveData();
      };

      BaseFormView.prototype.saveData = function() {
        var promise,
          _this = this;
        this.modelUnbind('change', this.render);
        this.fillModel(this.$form);
        this.disableActions();
        promise = $.Deferred().done();
        if (this.model && this.saveAsJSON) {
          promise = this.model.saveAsJSON();
        } else if (this.model) {
          promise = this.model.save();
        }
        promise.done(function() {
          return _this.trigger('model:saved');
        });
        promise.fail(function() {
          return _this.trigger('model:notSaved');
        });
        return promise.always(function() {
          return _this.enableActions();
        });
      };

      BaseFormView.prototype.dispose = function() {
        var prop, propertiesToReset, _i, _len, _results;
        BaseFormView.__super__.dispose.apply(this, arguments);
        propertiesToReset = ['successMessage', 'formSelector', 'formEvent', 'firstFocus', 'formData', 'validationRules', 'validationMessages', 'validationGroups'];
        _results = [];
        for (_i = 0, _len = propertiesToReset.length; _i < _len; _i++) {
          prop = propertiesToReset[_i];
          _results.push(delete this[prop]);
        }
        return _results;
      };

      return BaseFormView;

    })(View);
  });

}).call(this);
